//
//  PYShowMediaController.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYShowMediaController.h"
#import "PYShowMediaCell.h"
#import "PYScreenRotateManager.h"
#import "PYShowMediaManager.h"
#import "PYShowMediaPresentAnimation.h"
#import "PYShowMediaDismissAnimation.h"
#import "PYShowMediaGestureDismissAnimation.h"
#import "PYMediaInfo.h"

@interface PYShowMediaController () <PYShowMediaCellDelegate>

@property (nonatomic, strong) UICollectionView *clView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
/**
 文件数组
 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/**
 当前第几张图片
 */
@property (nonatomic, assign) NSUInteger index;

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, assign) BOOL exitWithoutCustomAnimation;
@property (nonatomic, assign) BOOL needScrollToIndex;
@property (nonatomic, assign) BOOL canScrollToIndex;
@end

@implementation PYShowMediaController

+ (instancetype)controllerWithArray:(NSArray *)dataArray index:(NSUInteger)index
{
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    manager.useCustomPresentAnimation = YES;
    manager.useGestureControlDismissAnimation = YES;
    manager.useCustomDismissAnimation = NO;
    
    if ([PYSystemTool isSimulator]) {
        NSAssert([dataArray isKindOfClass:[NSArray class]] && dataArray.count > 0 && index < dataArray.count, @"显示图片数据异常");
    } else {
        NSLog(@"dataArray.count = %ld, index = %lu", (long)dataArray.count, (unsigned long)index);
    }
    if (index >= dataArray.count) {
        index = 0;
    }
    PYShowMediaController *controller = [super new];
    controller.dataArray = [NSMutableArray arrayWithArray:dataArray];
    controller.index = index;
    return controller;
}

/**
 更新图片数组
 
 @param dataArray 图片数组，新数据
 @param index 当前显示第几张图片
 */
- (void)reloadWithDataArray:(NSArray *)dataArray index:(NSUInteger)index
{
    if ([PYSystemTool isSimulator]) {
        NSAssert([dataArray isKindOfClass:[NSArray class]] && dataArray.count > 0 && index < dataArray.count, @"显示图片数据异常");
    } else {
        NSLog(@"dataArray.count = %ld, index = %lu", (long)dataArray.count, (unsigned long)index);
    }
    if (index >= dataArray.count) {
        index = 0;
    }
    self.dataArray = [NSMutableArray arrayWithArray:dataArray];
    if ([self needPageControl]) {
        self.pageControl.numberOfPages = dataArray.count;
    }
    [self.clView reloadData];
    [self scrollToIndex:index];
}

- (void)scrollToIndex:(NSUInteger)index
{
    self.index = index;
    if (self.index < self.dataArray.count) {
        self.needScrollToIndex = YES;
        if (self.canScrollToIndex == NO) {
            return;
        }
        [self.clView setContentOffset:CGPointMake(index * self.layout.itemSize.width, 0)];
        [self.clView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically animated:NO];
        self.needScrollToIndex = NO;
        if ([self needPageControl]) {
            self.pageControl.currentPage = self.index;
        }
    }
}

- (nullable PYShowMediaCell *)getCurrentCell
{
    PYShowMediaCell *cell = (PYShowMediaCell *)[self.clView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_index inSection:0]];
    return cell;
}

/**
 获取当前显示的图片
 
 @return image
 */
- (nullable UIImage *)getCurrentImage
{
    PYShowMediaCell *cell = (PYShowMediaCell *)[self.clView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_index inSection:0]];
    if (!cell) {
        return nil;
    }
    return cell.imgView.image;
}
/**
 获取当前显示的图片对应的模型
 
 @return 七牛图片模型
 */
- (nullable PYMediaInfo *)getCurrentModel
{
    if (_index >= self.dataArray.count || _index < 0) {
        return nil;
    }
    return [self.dataArray objectAtIndex:_index];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self initClView];
    if ([self needPageControl]) {
        [self initPageControl];
    }
    [self reloadWithDataArray:self.dataArray index:self.index];
    self.transitioningDelegate = self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([self isPush]) {
        [PYSystemTool showLightStatus];
    } else {
        [PYSystemTool hideStatus];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 设置特殊旋转
    [self setupSpecialScreenRotate];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [PYSystemTool showDefaultStatus];
    if ([PYSystemTool isIPhone] && [UIDevice currentDevice].orientation != UIDeviceOrientationPortrait) {
        [PYSystemTool rotateDeviceToOrientation:UIDeviceOrientationPortrait];
    }
    // 恢复屏幕旋转设置
    [self recoverScreenRotate];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)viewSafeAreaInsetsDidChange
{
    [super viewSafeAreaInsetsDidChange];
    if ([self needPageControl]) {
        [self.pageControl mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-10 - self.view.top - self.view.safeAreaInsets.bottom);
        }];
    }
}

#pragma mark - 通知
- (void)addNotifications
{
    [super addNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shortCutNotification) name:PYShortCutNotification object:nil];
}

- (void)shortCutNotification
{
    [self dismissViewControllerAnimated:NO completion:nil];
    [PYSystemTool showDefaultStatus];
}

#pragma mark - pageControl
- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [UIPageControl new];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:1 alpha:0.5];
        _pageControl.hidesForSinglePage = YES;
        _pageControl.enabled = NO;
    }
    return _pageControl;
}

- (void)initPageControl
{
    [self.view addSubview:self.pageControl];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-10);
    }];
}

- (BOOL)needPageControl
{
    return YES;
}

#pragma mark - clView
- (UICollectionView *)clView
{
    if (!_clView) {
        _layout = [UICollectionViewFlowLayout new];
        _layout.itemSize = CGSizeMake(APP_WIDTH + show_image_margin, APP_HEIGHT);
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 0;
        _clView = [[UICollectionView alloc] initWithFrame:CGRectMake(-show_image_margin / 2, -self.view.top, APP_WIDTH + show_image_margin, APP_HEIGHT) collectionViewLayout:_layout];
        _clView.delegate = self;
        _clView.dataSource = self;
        _clView.showsHorizontalScrollIndicator = NO;
        _clView.pagingEnabled = YES;
        _clView.backgroundColor = [UIColor clearColor];
        [_clView registerClass:[PYShowMediaCell class] forCellWithReuseIdentifier:@"cellid"];
        if (@available(iOS 11.0, *)) {
            _clView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (self.navigationController.interactivePopGestureRecognizer) {
            [_clView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
        }
        [_clView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _clView;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (object == _clView && [keyPath isEqualToString:@"contentSize"]) {
        NSValue *newValue = [change objectForKey:NSKeyValueChangeNewKey];
        CGSize contentSize = [newValue CGSizeValue];
        if (!CGSizeEqualToSize(contentSize, CGSizeZero)) {
            self.canScrollToIndex = YES;
            if (self.needScrollToIndex) {
                [self scrollToIndex:self.index];
            }
        }
    }
}

- (void)initClView
{
    [self.view addSubview:self.clView];
    [self.clView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(-show_image_margin / 2);
        make.top.mas_equalTo(-self.view.top);
        make.width.mas_equalTo(APP_WIDTH + show_image_margin);
        make.height.mas_equalTo(APP_HEIGHT);
    }];
}

#pragma mark - UICollectionView 代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (PYShowMediaCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    PYShowMediaCell *cell = [PYShowMediaCell cellWithCollectionView:collectionView indexPath:indexPath];
    cell.delegate = self;
    cell.mediaInfo =  [self.dataArray objectAtSafeIndex:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    PYShowMediaCell *controllerell = (PYShowMediaCell *)cell;
    [controllerell.scrollView setZoomScale:1 animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //    [self endDecelerate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate == NO) {
        //        [self endDecelerate];
    }
}

//- (void)endDecelerate
//{
//    // 子类重写
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSUInteger index = round(scrollView.contentOffset.x / scrollView.frame.size.width);
    if (index != _index) {
        [self indexValueChangedByScroll:index];
    }
}

- (void)indexValueChangedByScroll:(NSUInteger)index
{
    _index = index;
    if ([self needPageControl]) {
        self.pageControl.currentPage = index;
    }
}

#pragma mark - 是否为push进来
- (BOOL)isPush
{
    return NO;
}

#pragma mark - PYShowMediaCellDelegate
- (void)collectionViewCell:(PYShowMediaCell *)cell panGestureDidPanGesture:(UIPanGestureRecognizer *)panGesture
{
    if ([self isPush]) {
        return;
    }
    CGPoint translation = [panGesture translationInView:self.view];
    //     NSLog(@"showImage translation = %@", NSStringFromCGPoint(translation));
    CGPoint velocity = [panGesture velocityInView:self.view];
    //     NSLog(@"showImage velocity = %@", NSStringFromCGPoint(velocity));
    [panGesture setTranslation:CGPointZero inView:self.view];
    
    static BOOL canPan = NO;
    static CGAffineTransform transform;
    static CGRect frame;
    if (panGesture.numberOfTouches > 1) {
        return;
    }
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    if (panGesture.state == UIGestureRecognizerStateBegan) {
//        NSLog(@"showImage begin numberOfTouches = %ld", panGesture.numberOfTouches);
//        NSLog(@"showImage velocity = %@", NSStringFromCGPoint(velocity));
//        NSLog(@"showImage translation = %@", NSStringFromCGPoint(translation));
        if (fabs(cell.scrollView.contentOffset.y) > 2) {
            return;
        }
        if (translation.y > 0) {
            CGFloat ratio = fabs(translation.x / translation.y);
            if (ratio < 0.5) {
                canPan = YES;
                transform = cell.imgView.transform;
                frame = cell.imgView.frame;
                self.clView.scrollEnabled = NO;
                manager.interactiveTransition = [UIPercentDrivenInteractiveTransition new];
                [self recoverScreenRotate];
                if (self.exitWithoutCustomAnimation == NO) {
                    [self dismissWithGestureControlAnimation];
                }
            } else {
                canPan = NO;
            }
        } else if (velocity.y > 0) {
            CGFloat ratio = fabs(velocity.x / velocity.y);
            if (ratio < 0.5) {
                // iPad/iPhone(有些设备) 中translation的值起始时总为CGPointZero
                canPan = YES;
                transform = cell.imgView.transform;
                frame = cell.imgView.frame;
                self.clView.scrollEnabled = NO;
                manager.interactiveTransition = [UIPercentDrivenInteractiveTransition new];
                [self recoverScreenRotate];
                if (self.exitWithoutCustomAnimation == NO) {
                    [self dismissWithGestureControlAnimation];
                }
            } else {
                canPan = NO;
            }
        } else {
            canPan = NO;
        }
    } else if (panGesture.state == UIGestureRecognizerStateChanged) {
        
        if (canPan == NO) {
            return;
        }
        //        NSLog(@"showImage changed numberOfTouches = %ld", panGesture.numberOfTouches);
        
//        NSLog(@"transform = %@", NSStringFromCGAffineTransform(transform));
        CGPoint center = cell.imgView.center;
        
        if (transform.a != 0) {
            center.x = cell.imgView.center.x / transform.a;
            center.x += translation.x / transform.a;
        }
        if (transform.d != 0) {
            center.y = cell.imgView.center.y / transform.d;
            center.y += translation.y / transform.d;
        }
        
        CGFloat maxCenterY = APP_HEIGHT;
        CGFloat minCenterY = APP_HEIGHT / 2;
        
//        NSLog(@"showImage center.y = %f", center.y);
//        NSLog(@"showImage maxCenterY = %f", maxCenterY);
//        NSLog(@"showImage minCenterY = %f", minCenterY);
//        NSLog(@"showImage self.clView.scrollEnabled = %d", self.clView.scrollEnabled);
//        NSLog(@"showImage cell.imgView.frame = %@", NSStringFromCGRect(cell.imgView.frame));
        if (center.y < maxCenterY && center.y > minCenterY) {
            CGFloat process = (center.y - minCenterY) / (maxCenterY - minCenterY);
//            NSLog(@"showImage process = %f", process);
            [manager.interactiveTransition updateInteractiveTransition:process];
            CGAffineTransform scaleTransform = CGAffineTransformScale(transform, 1 - process * (1 - 0.5 / cell.imgView.transform.a), 1 - process * (1 - 0.5 / cell.imgView.transform.d));
            CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(0, -process * (1 - 0.5 / cell.imgView.transform.d) / 2 * cell.imgView.frame.size.height);
            CGAffineTransform concatTransform = CGAffineTransformConcat(scaleTransform, translationTransform);
            cell.imgView.transform = concatTransform;
        }
        center.x *= transform.a;
        center.y *= transform.d;
        
        cell.imgView.center = center;
        //        NSLog(@"showImage changed velocity = %@", NSStringFromCGPoint(velocity));
    } else {
        if (canPan == NO) {
            return;
        }
        canPan = NO;
        //        NSLog(@"showImage end numberOfTouches = %ld", panGesture.numberOfTouches);
        
//        NSLog(@"showImage end velocity = %@", NSStringFromCGPoint(velocity));
        if (velocity.y > 0) {
            if (self.exitWithoutCustomAnimation) {
                // 触发dismiss转场动画
                [self dismissWithoutCustomAnimation];
            }
            
            UIImageView *imgView = [UIImageView new];
            imgView.image = cell.imgView.image;
            imgView.contentMode = UIViewContentModeScaleAspectFill;
            imgView.transform = cell.imgView.transform;
            imgView.frame = cell.imgView.frame;
            imgView.layer.masksToBounds = YES;
            
            [[UIApplication sharedApplication].keyWindow addSubview:imgView];
            cell.imgView.hidden = YES;
            [UIView animateWithDuration:PY_Animation_Duration animations:^{
                PYMediaInfo *mediaInfo = [self getCurrentModel];
                if (!CGRectEqualToRect(mediaInfo.imgViewFrame, CGRectZero)) {
                    imgView.frame = mediaInfo.imgViewFrame;
                    imgView.layer.cornerRadius = mediaInfo.imgViewCornerRadius;
                } else {
                    imgView.alpha = 0;
                }
            } completion:^(BOOL finished) {
                if (finished) {
                    [imgView removeFromSuperview];
                }
            }];
            [manager.interactiveTransition finishInteractiveTransition];
        } else {
            [UIView animateWithDuration:PY_Animation_Duration animations:^{
                cell.imgView.transform = CGAffineTransformIdentity;
                cell.imgView.transform = transform;
                cell.imgView.frame = frame;
            }];
            [manager.interactiveTransition cancelInteractiveTransition];
            [self setupSpecialScreenRotate];
            self.clView.scrollEnabled = YES;
        }
        
        manager.interactiveTransition = nil;
    }
}

- (void)singleTapGestureDidTap:(PYShowMediaCell *)cell
{
    NSLog(@"触发了点击事件");
    [self singleTap];
}

- (void)singleTap
{
    [self recoverScreenRotate];
    if ([PYSystemTool isIPhone] && self.exitWithoutCustomAnimation) {
        [self dismissWithoutCustomAnimation];
    } else {
        [self dismissWithCustomAnimation];
    }
}

- (void)longPressGestureDidPress:(PYShowMediaCell *)cell
{
    [self longPressAction];
}

- (void)longPressAction
{

}

#pragma mark - dismiss退出界面
/// dismiss退出图片浏览，不带自定义动画
- (void)dismissWithoutCustomAnimation
{
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    manager.useGestureControlDismissAnimation = NO;
    manager.useCustomDismissAnimation = NO;
    [self dismissViewControllerAnimated:YES completion:nil];
}

/// dismiss退出界面，带自定义动画
- (void)dismissWithCustomAnimation
{
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    manager.useGestureControlDismissAnimation = NO;
    manager.useCustomDismissAnimation = YES;
    [self dismissViewControllerAnimated:YES completion:nil];
}

/// dismiss退出界面，带手势控制的自定义动画
- (void)dismissWithGestureControlAnimation
{
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    manager.useGestureControlDismissAnimation = YES;
    manager.useCustomDismissAnimation = NO;
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"showImage 触发dismiss转场动画");
}

#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    if ([presented isKindOfClass:[PYShowMediaController class]] && manager.useCustomPresentAnimation) {
        return [PYShowMediaPresentAnimation new];
    }
    return nil;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    PYShowMediaManager *manager = [PYShowMediaManager sharedManager];
    if ([dismissed isKindOfClass:[PYShowMediaController class]]) {
        if (manager.useGestureControlDismissAnimation) {
            return [PYShowMediaGestureDismissAnimation new];
        } else if (manager.useCustomDismissAnimation) {
            return [PYShowMediaDismissAnimation new];
        }
    }
    return nil;
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator
{
    return [PYShowMediaManager sharedManager].interactiveTransition;
}

#pragma mark - 屏幕旋转
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    NSInteger index = _index;
    PYShowMediaCell *cell = (PYShowMediaCell *)[self.clView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    
    CGRect frame = [cell.scrollView convertRect:cell.imgView.frame toView:[UIApplication sharedApplication].keyWindow];
    UIImageView *imgView = [UIImageView new];
    imgView.image = cell.imgView.image;
    imgView.transform = CGAffineTransformIdentity;
    imgView.transform = cell.imgView.transform;
    imgView.frame = frame;
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [[UIApplication sharedApplication].keyWindow addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    self.clView.hidden = YES;
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        imgView.transform = CGAffineTransformIdentity;
        [[UIApplication sharedApplication].keyWindow layoutIfNeeded];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.clView.hidden = NO;
        self.clView.frame = CGRectMake(-show_image_margin / 2, 0, size.width + show_image_margin, size.height);
        self.layout.itemSize = CGSizeMake(size.width + show_image_margin, size.height);
        [self.clView reloadData];
        cell.imgView.transform = CGAffineTransformIdentity;
        if (index < self.dataArray.count) {
            [self.clView performBatchUpdates:^{
                [self.clView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically animated:NO];
            } completion:^(BOOL finished) {
                if (finished) {
                    [imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        
                    }];
                    [imgView removeFromSuperview];
                }
            }];
        }
    }];
    
    if (size.width > size.height) {
        if ([PYSystemTool isIPhone]) {
            self.exitWithoutCustomAnimation = YES;
        } else {
            self.exitWithoutCustomAnimation = NO;
        }
        if (self.navigationController.interactivePopGestureRecognizer) {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
            //            [self.navigationController.interactivePopGestureRecognizer requireGestureRecognizerToFail:self.clView.panGestureRecognizer];
        }
    } else {
        self.exitWithoutCustomAnimation = NO;
        if (self.navigationController.interactivePopGestureRecognizer) {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
            [self.clView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
        }
    }
}

/// 设置特殊旋转
- (void)setupSpecialScreenRotate
{
    PYScreenRotateConfig *config = [PYScreenRotateConfig new];
    config.shouldIPadAutorotate = YES;
    config.shouldIPhoneAutorotate = YES;
    config.iPadSupportedInterfaceOrientations = UIInterfaceOrientationMaskAll;
    config.iPhoneSupportedInterfaceOrientations = UIInterfaceOrientationMaskAll;
    [PYScreenRotateManager makeConfigWithConfig:config];
}

/// 恢复屏幕旋转设置
- (void)recoverScreenRotate
{
    [PYScreenRotateManager makeDefaultConfig];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSLog(@"图片查看器内存不足");
}

#pragma mark - MLeaksFinder methods
- (BOOL)willDealloc {
    return NO;
}
@end
